//
//  SortMenuView.m
//  shiweiquanjing
//
//  Created by xftt on 14-7-10.
//
//

#import "SortMenuView.h"

@implementation SortMenuView
{
    UITableView *tbSort;
    NSDictionary *qjDic;
    UIButton *btnEdit;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIView *backView = [[UIView alloc]initWithFrame:frame];
        [backView setBackgroundColor:[UIColor blackColor]];
        [backView setAlpha:0.1];
        [self addSubview:backView];
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(148, 17, 364, 544)];
        line.layer.cornerRadius = 15;
        [line setBackgroundColor:[UIColor colorWithHexString:@"#333333"]];
        [self addSubview:line];
        UIView *menu = [[UIView alloc] initWithFrame:CGRectMake(150, 19, 360, 540)];
        menu.layer.cornerRadius = 15;
        [menu setBackgroundColor:[UIColor colorWithHexString:@"#444444"]];
        [self addSubview:menu];
        UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 320, 20)];
        [title setBackgroundColor:[UIColor clearColor]];
        [title setText:@"请选择分类目录"];
        [title setFont:[UIFont systemFontOfSize:20]];
        [title setTextAlignment:NSTextAlignmentCenter];
        [title setTextColor:[UIColor colorWithHexString:@"#AAAAAA"]];
        [menu addSubview:title];
        tbSort = [[UITableView alloc] initWithFrame:CGRectMake(20, 50, 320, 408)];
        [tbSort setBackgroundColor:[UIColor colorWithHexString:@"#555555"]];
        [tbSort setDataSource:self];
        [tbSort setDelegate:self];
        [tbSort setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        [menu addSubview:tbSort];
        UIButton *btnEsc = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnEsc setFrame:CGRectMake(54, 476, 104, 44)];
        [btnEsc setTitle:@"取消" forState:UIControlStateNormal];
        [btnEsc.titleLabel setFont:[UIFont systemFontOfSize:17]];
        [btnEsc setTitleColor:[UIColor colorWithHexString:@"#AAAAAA"] forState:UIControlStateNormal];
        [btnEsc setBackgroundColor:[UIColor colorWithHexString:@"#555555"]];
        [btnEsc addTarget:self action:@selector(doEdit:) forControlEvents:UIControlEventTouchUpInside];
        [btnEsc setTag:0];
        [menu addSubview:btnEsc];
        btnEdit = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnEdit setFrame:CGRectMake(202, 476, 104, 44)];
        [btnEdit setTitle:@"确定" forState:UIControlStateNormal];
        [btnEdit.titleLabel setFont:[UIFont systemFontOfSize:17]];
        [btnEdit setTitleColor:[UIColor colorWithHexString:@"#999999"] forState:UIControlStateNormal];
        [btnEdit setBackgroundColor:[UIColor colorWithHexString:@"#666666"]];
        [btnEdit addTarget:self action:@selector(doEdit:) forControlEvents:UIControlEventTouchUpInside];
        [btnEdit setTag:1];
        [menu addSubview:btnEdit];
    }
    return self;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_QJSort count]+1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = @"SCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil){
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    [cell setBackgroundColor:[UIColor clearColor]];
    cell.selectedBackgroundView = [[UIView alloc] initWithFrame:cell.frame];
    cell.selectedBackgroundView.backgroundColor = [UIColor colorWithHexString:@"#4B4B4B"];
    [cell.textLabel setFont:[UIFont systemFontOfSize:17]];
    [cell.textLabel setTextColor:[UIColor colorWithHexString:@"#AAAAAA"]];
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(5, 49, cell.frame.size.width, 1)];
    [line setBackgroundColor:[UIColor colorWithHexString:@"#484848"]];
    [cell addSubview:line];}
    [cell.imageView setImage:[UIImage imageNamed:@"3"]];
    if(indexPath.row == 0){
    [cell.textLabel setText:@"暂不分类"];
    }else{
    [cell.textLabel setText:[[_QJSort objectAtIndex:indexPath.row-1] objectForKey:@"sortname"]];
    }return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0){
    NSMutableDictionary *qjing = [NSMutableDictionary dictionary];
    [qjing setValue:@"" forKey:@"sortid"];
    [qjing setValue:@"" forKey:@"sortname"];
    qjDic = qjing;
    }else{
        qjDic = [_QJSort objectAtIndex:indexPath.row-1];
    }
    [btnEdit setTitleColor:[UIColor colorWithHexString:@"#AAAAAA"] forState:UIControlStateNormal];
    [btnEdit setBackgroundColor:[UIColor colorWithHexString:@"#555555"]];
}

-(void)doEdit:(UIButton*)sender
{
    if (sender.tag==1) {
        if (qjDic != nil) {
            NSMutableDictionary *qjing = [NSMutableDictionary dictionaryWithDictionary:_quanjing];
            [qjing setValue:[qjDic objectForKey:@"sortid"] forKey:@"sortid"];
            [qjing setValue:[qjDic objectForKey:@"sortname"] forKey:@"sortname"];
            [self.FController saveSort:qjing from:_quanjing];
            [self removeFromSuperview];
        }
    }
    else
    {
        [self removeFromSuperview];
    }
}
@end
